import java.util.PriorityQueue;

public class MinimumOperationsHalveArraySum {
    public int halveArray(int[] nums) {
        // 构建大根堆
        PriorityQueue<Double> queue = new PriorityQueue<Double>((o1, o2) -> o2.compareTo(o1));
        // 记录所给元素总和
        double total = 0;
        for (int x : nums) {
            total += x;
            queue.offer((double)x);
        }

        // 记录目标值, 其结果要第一次大于它
        total /= 2.0;

        int ret = 0;
        double sum = 0;
        // 当减小的总值超过初始数组和的一半, 结束循环
        while (sum < total) {
            double t = queue.poll() / 2.0;
            sum += t;
            queue.offer(t);
            ret++;
        }
        return ret;
    }
}
